package com.mdxx.xczd.app;

import android.app.AlarmManager;
import android.app.Application;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import com.mdxx.xczd.bean.BarCodeBean;
import com.mdxx.xczd.bean.Point;
import com.mdxx.xczd.bean.TaskBean;
import com.mdxx.xczd.bean.UserBean;
import com.mdxx.xczd.utils.CrashHandler;
import com.mdxx.xczd.utils.NetworkUtil;
import com.orhanobut.logger.Logger;
import java.util.Locale;
import cn.jpush.android.api.JPushInterface;

/**
 * Created by MD-02 on 2017/1/13.
 */

public class App extends Application{

    private static final String TAG = "daize";
    private static Context context;
    public static String USERID = "";
   /* public static SharedPreferences sp = null;*/
    public static UserBean userBean;
    public static TaskBean taskBean;
    public static String alarmNum = null;
    public static Point mPoint = new Point(0,0);
    public static int batteryLevel = 100;  //电量
    public static float radius = -1f;  //精度
    public static int dbm = 0;
    public static String firstUrl;
    public static BarCodeBean barCodeBean;
    public static String addrstr;
   /* private static RealmConfiguration configuration;*/
    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        Logger.init(TAG);
        CrashHandler.getInstance().init(this);
        initJpush();
        uncaughtExceptionRestart();
        initVlc();
       /* initRealm();*/
    }

   /* private void initRealm(){
        configuration = new RealmConfiguration.Builder(this)
                .name("name.realm")
                .build();
    }

    public static RealmConfiguration getRealmConfig(){
        return configuration;
    }*/

    private void initVlc(){
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
        String p = pref.getString("set_locale", "");
        if (p != null && !p.equals("")) {
            Locale locale;
            // workaround due to region code
            if(p.equals("zh-TW")) {
                locale = Locale.TRADITIONAL_CHINESE;
            } else if(p.startsWith("zh")) {
                locale = Locale.CHINA;
            } else {
                locale = new Locale(p);
            }
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config,
                    getBaseContext().getResources().getDisplayMetrics());
        }
    }

    public static void cancelAccount(){
        USERID = "";
        userBean = null;
        taskBean = null;
    }

    public static Context getAppContext(){
        return context;
    }

    public static Resources getAppResources() {
        return context.getResources();
    }

    public static void setJushData(String key){
        ApplicationInfo appInfo = null;
        try {
            appInfo = context.getPackageManager()
                    .getApplicationInfo(context.getPackageName(),PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        appInfo.metaData.putString("JPUSH_APPKEY", key);
        initJpush();
    }

    private static void initJpush(){
        JPushInterface.init(context);
        JPushInterface.setAlias(context, NetworkUtil.getAndroid_id(), null);
    }

    public void uncaughtExceptionRestart(){
        Intent intent = new Intent();
        intent.setClassName("com.mdxx.xczd", "com.mdxx.xczd.SelectUserActivity");
        final PendingIntent restartIntent = PendingIntent.getActivity(getApplicationContext(), -1, intent,
                PendingIntent.FLAG_ONE_SHOT);
        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread thread, Throwable ex) {
                AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, restartIntent);
                android.os.Process.killProcess(android.os.Process.myPid());
            }
        });
    }
}
